//
//  AppDelegate.swift
//  Sina15
//
//  Created by 盼 on 16/4/6.
//  Copyright © 2016年 pan. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        //创建window对象
        window = UIWindow()
        
        
        //注册通知
        registerNotification()
        
        //设置window对象的根控制器
//        window?.rootViewController = SLMainController()
//        window?.rootViewController = SLWelcomeController()
        
        //设置window的rootViewController
        window?.rootViewController = defaultRootViewController()
        
        //把window设置为主窗口并且可见
        window?.makeKeyAndVisible()
        
        //设置导航条的背景色
        setupNavBarThemeColor()
        
        return true
    }
    
    func defaultRootViewController()->UIViewController {
        
        print(SLUserViewModel.sharedModel.userLogin)
        
        return SLUserViewModel.sharedModel.userLogin ? SLWelcomeController() : SLMainController()
    }
    
    func setupNavBarThemeColor(){
        
        //全局颜色的设置，在控件实例化之前
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
    }
    
    func registerNotification() {
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "changeRootViewController:", name:switchRootViewController , object: nil)
    }
    
    //接收到通知后处理的方法
    func changeRootViewController(notification: NSNotification) {
        
        if notification.object != nil {
            
            //从欢迎页面发出的通知
            window?.rootViewController = SLMainController()
        }else{
            //从授权页面发出的通知
            window?.rootViewController = SLWelcomeController()
        }
    }

    func applicationWillResignActive(application: UIApplication) {

    }

    func applicationDidEnterBackground(application: UIApplication) {

    }

    func applicationWillEnterForeground(application: UIApplication) {

    }

    func applicationDidBecomeActive(application: UIApplication) {

    }

    func applicationWillTerminate(application: UIApplication) {

    }


}

